﻿Public Class frmModifListeNoire
    Dim dbListeNoire As New cls_taco
    Dim idClient As Integer
    Dim dateDebut As DateTime
    Dim messageErreur As String

    Private Sub frmModifListeNoire_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
    End Sub

    Public Overloads Function ShowDialog(ByRef idDuClient, ByVal v_dateDebut) As Windows.Forms.DialogResult


        If idDuClient <> Nothing And v_dateDebut <> Nothing Then
            idClient = idDuClient
            dateDebut = v_dateDebut
            remplirinfo()
            btnAjouter.Text = "Enregistrer"
            dtDebut.Enabled = False

        Else
            remplirInfoParDefaut()
            btnAjouter.Text = "Ajouter"
        End If


        Return MyBase.ShowDialog()
    End Function

    Private Sub frmModifListeNoire_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       
    End Sub

    Public Sub remplirinfo()
        'créé la connection à la base de données
        dbListeNoire = New cls_taco

        dbListeNoire.defDataset("SELECT noClient,dateDebut, duree, commentaire from tblListeNoire where noClient = " & idClient & " and dateDebut = #" & FormatDateTime(dateDebut, DateFormat.ShortDate) & "#", "tblListeNoire")

        For Each colonne As DataColumn In dbListeNoire.ds.Tables("tblListeNoire").Columns

            With dbListeNoire.ds.Tables("tblListeNoire")
                Select Case colonne.ColumnName


                    Case "commentaire"
                        txtCommentaire.Text = nvl(.Rows(0).Item("commentaire"), "")
                    Case "duree"
                        txtDuree.Text = nvl(.Rows(0).Item("duree"), "")

                    Case "dateDebut"
                        dtDebut.Value = nvl(.Rows(0).Item("dateDebut"), "")

                End Select
            End With
        Next

        updateInfoClient(idClient)
    End Sub

    Sub updateInfoClient(ByVal v_idClient)
        idClient = v_idClient
        Dim dbClients = New cls_taco
        Dim nom As String = ""
        Dim prenom As String = ""
        dbClients.defDataset("SELECT nom, prenom, adresse, telephone " _
       & "FROM tblClients where noClient = " & v_idClient & ";", "tblClients")
        'vide la listView


        'créé une colonne dans la listView pour chaque élément de la requête SQL
        For Each colonne As DataColumn In dbClients.ds.Tables("tblClients").Columns
            'For i = 0 To dsClients.Tables("tblClients").Columns.Count
            '    MsgBox(dsClients.Tables("tblClients").Rows(0).Item(i))
            'Next
            With dbClients.ds.Tables("tblClients")
                Select Case colonne.ColumnName

                    Case "nom"
                        nom = nvl(.Rows(0).Item("nom"), "")
                    Case "prenom"
                        prenom = nvl(.Rows(0).Item("prenom"), "")
                    Case "adresse"
                        txtAdresse.Text = nvl(.Rows(0).Item("adresse"), "")
                    Case "telephone"
                        txtTel.Text = nvl(.Rows(0).Item("telephone"), "")
                End Select
            End With
        Next

        TxtNom.Text = prenom & " " & nom

        'créé un élément dans le listView pour chaque enregistrement retourné par la requête 
    End Sub



    Public Sub remplirInfoParDefaut()

        dtDebut.Enabled = True
        dtFin.Enabled = True


    End Sub
    Public Function validerDonnee()
        Return validerDuree(CInt(txtDuree.Text), messageErreur, False) And idClient <> Nothing
      
    End Function


    Private Sub btnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter.Click
        messageErreur = "Erreur: "

        If validerDonnee() Then
            Try
                If Not ObjHasNoIn(CInt(idClient), "noClient", "tblLocations", True) Then

                    If btnAjouter.Text = "Enregistrer" Then
                        For Each listeNoireEnCours As DataRow In dbListeNoire.ds.Tables("tblListeNoire").Rows
                            With listeNoireEnCours
                                .Item("dateDebut") = dtDebut.Value

                                .Item("commentaire") = txtCommentaire.Text
                                .Item("noClient") = idClient
                                .Item("duree") = txtDuree.Text
                            End With
                        Next

                        dbListeNoire.update("tblListeNoire")

                    ElseIf btnAjouter.Text = "Ajouter" Then

                        ajouterClientLstNoire(dtDebut.Value, idClient, CInt(txtDuree.Text), txtCommentaire.Text)


                    End If
                    If frmGestionListeNoire.IsHandleCreated = True Then frmGestionListeNoire.btnRechercher.PerformClick()
                    Me.Close()
                Else
                    MsgBox("Impossible de bannir quelqu'un qui a une location active")
                End If

            Catch oled As OleDb.OleDbException
                MsgBox("Il est impossible d'ajouter ce client avec les paramètres définis sur la liste noire, vérifier si une entrée existante n'ai pas la même date ni le même client...")

            End Try
        Else
            MsgBox(messageErreur)
        End If

    End Sub

    Private Sub txtDuree_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDuree.KeyPress
        If Not IsNumeric(e.KeyChar) And e.KeyChar <> ControlChars.Back And e.KeyChar <> ControlChars.Tab Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtDuree_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDuree.TextChanged
        If txtDuree.Text <> Nothing Then
            dtFin.Value = dtDebut.Value.AddDays(CInt(txtDuree.Text))
            dtFin.MinDate = dtDebut.Value
        End If
    End Sub

    Private Sub dtFin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtFin.ValueChanged

        txtDuree.Text = DateDiff(DateInterval.Day, dtDebut.Value, dtFin.Value)

    End Sub

    Private Sub dtDebut_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtDebut.ValueChanged

        txtDuree.Text = DateDiff(DateInterval.Day, dtDebut.Value, dtFin.Value)
    End Sub

    Private Sub btnRechercher_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechercher.Click
        frmGestionClients.chkActif.Checked = True
        frmGestionClients.chkActif.Enabled = False
        frmGestionClients.ShowDialog(True)

    End Sub

    Private Sub btnAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnnuler.Click
        Me.Close()
    End Sub

End Class